package org.csu.goodpartner.controller;

import org.csu.goodpartner.VO.LineItemVO;
import org.csu.goodpartner.VO.OrderVO;
import org.csu.goodpartner.common.CommonResponse;
import org.csu.goodpartner.entity.LineItem;
import org.csu.goodpartner.service.LineItemService;
import org.csu.goodpartner.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/*
 出库业务：
 1、商家发货
 2、接受订单
 3、接受取消订单

 退货业务：修改订单status
 1、商家同意退货流程
 2、商家拒绝退货流程
 */
@CrossOrigin
@Controller
public class OrderController {
    public static final int STATUS0 = 0;
    public static final int STATUS1 = 1;
    public static final int STATUS2 = 2;
    public static final int STATUS3 = 3;
    public static final int STATUS4 = 4;
    public static final int STATUS5 = 5;
    public static final int STATUS6 = 6;

    @Autowired
    private OrderService orderService;
    @Autowired
    private LineItemService lineItemService;

    //展示订单 /shop/{shopId}/order/orders
//    @ResponseBody
//    @PostMapping("/shop/{shopId}/order/orders")
//    //@Scheduled(fixedRate = 2000)
//    public CommonResponse<List<OrderVO>> getAllOrders(@PathVariable int shopId){
//        try{
//            return orderService.getAllOrders(shopId);
//        }catch(Exception e){
//            e.printStackTrace();
//            return CommonResponse.createForError("出现错误，请刷新重试");
//        }
//    }

    /*
     分状态展示订单 /shop/{shopId}/order/order/status/{status}
     0：未发货；1：已发货；2：取消订单；3：退货；4：待商家确认订单；
     5：待商家确认退货；6：取消退货；7：初始值；
    */
    @ResponseBody
    @PostMapping("/shop/{shopId}/order/status/{status}")

    public CommonResponse<List<OrderVO>> getOrderByStatus(@PathVariable int shopId,@PathVariable int status){
        try{
            return orderService.getOrderByStatus(shopId,status);

        }catch(Exception e){
            e.printStackTrace();
            return CommonResponse.createForError("出现错误，请刷新重试");
        }
    }

    //模糊查询 /shop/{shopId}/order/select
    @ResponseBody
    @PostMapping("/shop/{shopId}/order/select")
    public CommonResponse<List<OrderVO>> getSelected(@PathVariable int shopId, @RequestBody Map map){
        try{
            return orderService.getOrderByLike(shopId,(String)map.get("params"));

        }catch(Exception e){
            e.printStackTrace();
            return CommonResponse.createForError("出现错误，请刷新重试");
        }
    }

    //查看某一具体订单 /shop/{shopId}/order/{orderId}
    @ResponseBody
    @PostMapping("/shop/{shopId}/order/{orderId}")
    public CommonResponse<List<LineItemVO>> getLineItemByOrderId(@PathVariable int orderId){
        try{
            return lineItemService.getLineItemByOrderId(orderId);

        }catch(Exception e){
            e.printStackTrace();
            return CommonResponse.createForError("出现错误，请刷新重试");
        }
    }

    //发货 /shop/{shopId}/order/status/0/deliver
    @ResponseBody
    @PostMapping("/deliver/{orderId}")
    public CommonResponse<List<OrderVO>> deliver(@PathVariable int orderId){
        try{
            return orderService.deliverOrder(orderId);

        }catch(Exception e){
            e.printStackTrace();
            return CommonResponse.createForError("出现错误，请刷新重试");
        }
    }

    //修改商品数量 /shop/{shopId}/order/{orderId}/updateQuantities
    @ResponseBody
    @PostMapping("/shop/{shopId}/order/{orderId}/updateQuantities")
    public CommonResponse updateQuantities(@RequestBody List<LineItem> lineItemList){
        try{
            System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            return lineItemService.updateQuanties(lineItemList);
        }catch(Exception e){
            e.printStackTrace();
            return CommonResponse.createForError("出现错误，请刷新重试");
        }
    }

    //接受订单 /shop/{shopId}/order/status/4/receive
    @ResponseBody
    @PostMapping("/receive/{orderId}")
    public CommonResponse receive(@PathVariable int orderId){
        try{
            return lineItemService.receiveOrder(orderId);
        }catch(Exception e){
            e.printStackTrace();
            return CommonResponse.createForError("出现错误，请刷新重试");
        }
    }

    //取消接受订单 /shop/{shopId}/order/status/0或4/cancel
    @ResponseBody
    @PostMapping("/cancel/{orderId}")
    public CommonResponse cancel(@PathVariable int orderId){
        try{
            return lineItemService.cancelOrder(orderId);
        }catch(Exception e){
            e.printStackTrace();
            return CommonResponse.createForError("出现错误，请刷新重试");
        }
    }

    //同意退货 5=退货请求
    @ResponseBody
    @PostMapping("/agree/{orderId}")
    public CommonResponse agree(@PathVariable int orderId){
        try{
            return lineItemService.agreeRefund(orderId);
        }catch(Exception e){
            e.printStackTrace();
            return CommonResponse.createForError("出现错误，请刷新重试");
        }
    }

    //不同意退货
    @ResponseBody
    @PostMapping("/reject/{orderId}")
    public CommonResponse reject(@PathVariable int orderId){
        try{
            return orderService.rejectRefund(orderId);
        }catch(Exception e){
            e.printStackTrace();
            return CommonResponse.createForError("出现错误，请刷新重试");
        }
    }
}
